package com.situ.mvc.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.mvc.entity.Department;
import com.situ.mvc.entity.Role;
import com.situ.mvc.entity.User;
import com.situ.mvc.entity.UsersPageBean;
import com.situ.mvc.service.IRoleService;
import com.situ.mvc.service.impl.RoleServiceImpl;

@Controller
@RequestMapping(value = "/role")
public class RoleController {
	
	private IRoleService roleService = new RoleServiceImpl();

	@RequestMapping(value = "/pageList")
	public String pageList(@RequestParam(defaultValue="1")Integer pageNo,@RequestParam(defaultValue="3")Integer pageSize,Model model){
		UsersPageBean usersPageBean = roleService.findPageList(pageNo,pageSize);
		model.addAttribute("usersPageBean", usersPageBean);
		return "role_pagelist";
	}
	
	@RequestMapping(value = "/getAddUserPage")
	public String getAddUserPage(){
		return "role_add";
	}
	
	@RequestMapping(value = "/selectDepartment")
	@ResponseBody
	public List<Department> selectDepartment(){
		List<Department> list = roleService.selectDepartment();
		return list;
	}
	
	@RequestMapping(value = "/addUser")
	public String addUser(User user){
		boolean result = false;
		result = roleService.addUser(user);
/*		if (result == false) {
			return;
		}*/
		return "redirect:/role/pageList.action";
	}
	
	@RequestMapping(value = "/toAddRole")
	public String toAddRole(Integer id,Model model){
		model.addAttribute("id", id);
		return "role_only_add";
	}
	
	@RequestMapping(value = "/selectRole")
	@ResponseBody
	public List<Role> selectRole(){
		List<Role> list = roleService.selectRole();
		return list;
	}
	
	@RequestMapping(value = "addOnlyRole")
	public String addOnlyRole(Integer userId,Integer roleId){
		boolean result = false;
		result = roleService.addOnlyRole(userId,roleId);
		return "redirect:/role/pageList.action";
	}
	
	@RequestMapping(value = "/deleteById")
	public String deleteById(Integer id){
		System.out.println("deleteId:"+id);
		boolean result = false;
		result = roleService.deleById(id);
		return "redirect:/role/pageList.action";
	}
	
	


}
